package youcaihuaoa.weetop.com.vm.apply

import android.app.Application
import androidx.databinding.ObservableField
import api.UserServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.model.ExamineBean
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse

class MyApplyListViewModel(application: Application) : GLBaseViewModel(application) {


    val tabType = ObservableField<Int>(TAB_PENDING_APPROVE)

    val uiChangeEvent = UiChangeEvent()
    class UiChangeEvent{
        val dataEvent = SingleLiveEvent<List<ExamineBean>>()
    }

    val clickPendingApproveTabCommand = BindingCommand<Any>(BindingAction {
        tabType.set(TAB_PENDING_APPROVE)
        examineUserData()
    })

    val clickApproveTabCommand = BindingCommand<Any>(BindingAction {
        tabType.set(TAB_APPROVE)
        examineUserData()
    })



    val clickApplyTabCommand = BindingCommand<Any>(BindingAction {
        tabType.set(TAB_APPLY)
    })

    fun examineUserData() {
        UserServiceFactory.applyUserData(
            if (tabType.get() == TAB_PENDING_APPROVE) {
                "1"
            } else {
                "2"
            }
        ).subscribe(object : AbstractViewModelSubscriber<BaseResponse<List<ExamineBean>>>(this) {
            override fun onNext(t: BaseResponse<List<ExamineBean>>) {
                uiChangeEvent.dataEvent.postValue(t.data)
            }
        })
    }

    companion object {

        const val TAB_PENDING_APPROVE = 101

        const val TAB_APPROVE = 102

        const val TAB_APPLY = 103


    }
}